package com.aspirecn.core.commons;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * HTTP请求和响应过程中的一些工具方法
 * @author wangxinning
 *
 */
public class HttpUtils {

	/**
	 * 获取整型参数
	 * 
	 * @param req
	 * @param para
	 * @param defValue
	 * @return
	 */
	public static int getIntParameter(HttpServletRequest req, String para,
			int defValue) {
		return NumberUtils.toInt(req.getParameter(para), defValue);
	}

	/**
	 * 获取浮点型参数
	 * 
	 * @param req
	 * @param para
	 * @param defValue
	 * @return
	 */
	public static float getFloatParameter(HttpServletRequest req, String para,
			float defValue) {
		return NumberUtils.toFloat(req.getParameter(para), defValue);
	}

	/**
	 * 获取boolean型变量
	 * 
	 * @param req
	 * @param para
	 * @param defValue
	 * @return
	 */
	public static boolean getBooleanParameter(HttpServletRequest req,
			String para, boolean defValue) {
		String tmp = req.getParameter(para);

		if (StringUtils.isEmpty(para)) {
			return defValue;
		} else {
			if (StringUtils.equalsIgnoreCase("true", tmp)) {
				return true;
			} else if (StringUtils.equalsIgnoreCase("false", tmp)) {
				return false;
			} else {
				return defValue;
			}
		}
	}

	/**
	 * 获取字符型参数
	 * 
	 * @param req
	 * @param para
	 * @param defValue
	 * @return
	 */
	public static String getStringParameter(HttpServletRequest req,
			String para, String defValue) {
		String tmp = req.getParameter(para);

		if (StringUtils.isBlank(tmp)) {
			return defValue;
		} else {
			return tmp;
		}
	}
	
	/**
	 * 忽略了大小写
	 * @param request
	 * @param headername
	 * @return
	 */
	public static String getHeaderIgnoreCase( HttpServletRequest request, String headername )
	{
		Enumeration<String> enumeration = request.getHeaderNames();
		if( enumeration != null )
		{
			while( enumeration.hasMoreElements() )
			{
				String tempName = enumeration.nextElement();
				if( StringUtils.equalsIgnoreCase(tempName, headername) )
				{
					return request.getHeader(tempName);
				}
			}
		}
		
		return null;
	}
	
	/**
	 * 与addCookie方法配合使用
	 * @param request
	 * @param name
	 * @return
	 */
	public static String getCookieIgnoreCase( HttpServletRequest request, String name )
	{
		Cookie[] cookieArray = request.getCookies();		
		if( ArrayUtils.isNotEmpty(cookieArray) )
		{
			for( Cookie c:cookieArray )
			{
				if( StringUtils.equalsIgnoreCase(c.getName(),name))
				{
					try
					{
						String cookieValue = URLDecoder.decode(
								c.getValue(), "utf-8");
						return cookieValue;
					}
					catch( Exception e )
					{
						//e.printStackTrace();
						return c.getValue();//异常则不转码
					}
				}
			}
		}
		
		return null;		
	}
	
	/**
	 * 添加cookie，内部处理，进行转码，确保内容中没有特殊字符
	 * @param response
	 * @param cookie
	 * @throws UnsupportedEncodingException
	 */
	public static void addCookie(HttpServletResponse response, Cookie cookie)
			throws UnsupportedEncodingException {
		if (cookie != null) {
			String value = cookie.getValue();
			if (StringUtils.isNotEmpty(value)) {
				//取出内容做编码
				value = URLEncoder.encode(value, "utf-8");
				cookie.setValue(value);
			}

			response.addCookie(cookie);
		}
	}
	
	
	public static void main( String[] args ) throws UnsupportedEncodingException
	{
		String a = "();$dasd as das 423424?!@#$% =///";
		System.out.println( URLEncoder.encode(a,"utf-8") );
		System.out.println( URLDecoder.decode(URLEncoder.encode(a,"utf-8"),"utf-8") );
	}
}